package com.jxd.hbase.analysis;

import com.jxd.entity.MesHead;
import com.jxd.entity.SelectionMess;
import com.jxd.entity.Switchover;
import com.jxd.service.TwoMachineSwitchoverService;
import com.jxd.service.impl.TwoMachineSwitchoverServiceImpl;
import org.apache.hadoop.hbase.client.Connection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class TwoMachineSwitchover {
    @Value("${hbase.bwcontent.tablename}")
    private String bwContentTableName;
    @Autowired
    private TwoMachineSwitchoverServiceImpl twoMachineSwitchoverService;
    public void  gettwoMachineSwitchover(String filename, String mesId,String workMesId, String shipId,String xtid, Connection connection){
        String tableName = bwContentTableName;
        System.out.println(tableName);
        System.out.println(mesId);
        System.out.println(workMesId);
        //获取当前的心跳报文
        List<MesHead> mesHeadList = twoMachineSwitchoverService.getHeartbeatMesBysendDevice(filename,shipId,mesId);
        System.out.println("mesHeadList.size()==="+mesHeadList.size());
        //检索所有的发送设备
        List<String> sendDevices = new ArrayList<>();
        for (int i = 0; i <mesHeadList.size() ; i++) {
            String  sendDevice = mesHeadList.get(i).getSendDevice();
            if (!sendDevices.contains(sendDevice)){
                sendDevices.add(sendDevice);
            }
        }
        System.out.println("sendDevices.size()=========="+sendDevices.size());

        //根据设备检索数组messignid
        for (int i = 0; i <sendDevices.size() ; i++) {
            List<MesHead> heartbeatMess = new ArrayList<>();
            for (int j = 0; j <mesHeadList.size() ; j++) {
                //根据设备名称查询所有的列表数组
                if (mesHeadList.get(j).getSendDevice().equals(sendDevices.get(i))){
                     //添加信息
                    heartbeatMess.add(mesHeadList.get(j));
                }
            }
            //同设备名下的心跳报文
            System.out.println("heartbeatMess.size()=="+heartbeatMess.size());;
            //获取心跳数组的报文 10 11 12
            List<Switchover> switchovers =
                    twoMachineSwitchoverService
                            .getMesContentByHeartbeatMess(heartbeatMess,connection,tableName);
            System.out.println("switchovers==="+switchovers.size());
            //获取双机切换的状态
            List<com.jxd.model.TwoMachineSwitchover> twoMachineSwitchovers =
                    twoMachineSwitchoverService
                            .getTwoMachineSwitchovers(switchovers);
            System.out.println("twoMachineSwitchovers.size()====="+twoMachineSwitchovers.size());
           //获取41040报文
            List<MesHead>selectionMessBysendDevices =
                    twoMachineSwitchoverService
                            .getSelectionMessBysendDevice
                                    (filename,shipId,workMesId,sendDevices.get(i));
            System.out.println("selectionMessBysendDevices==="+selectionMessBysendDevices.size());
            List<SelectionMess> selectionMessContent=
                    twoMachineSwitchoverService
                            .getSelectionMessContent
                                    (selectionMessBysendDevices, tableName, connection);
            System.out.println("selectionMessContent==="+selectionMessContent.size());
            //获取工作时间
           String workingTime =
                    twoMachineSwitchoverService
                            .getWorkingTime(selectionMessContent);
            System.out.println("workingTime====="+workingTime);
            //获取加电时间
            String powerOnTime = twoMachineSwitchoverService.getPowerOnTime( heartbeatMess);
            System.out.println("powerOnTime====="+powerOnTime);
            twoMachineSwitchoverService
                    .insertTwoMachineSwitchover
                            (twoMachineSwitchovers,workingTime,powerOnTime,shipId, filename, xtid);
        }

    }
}
